package com.ynny.service.impl;

import com.ynny.dao.UserDao;
import com.ynny.entity.User;
import com.ynny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Transactional
    @Override
    public int checkUser(Integer userId, Integer price) {

        //3.1查询用户是否存在
        User user = userDao.selectUserById(userId);
        if (user == null) {
            return -4;//用户不存在
        }
        //3.2 余额是否充足
        if(user.getBalance() - price < 0) {
            return -5;//余额不足
        }
        //3.3修改用户的余额
        int res = userDao.updateBalance(userId, price);

        if (res != 1){
            return -6;//服务器异常 更新余额失败
        }

        return 1; //购买成功
    }
}
